//
//  YPLoginBaseClass.m
//
//  Created by   on 2019/2/24
//  Copyright (c) 2019 __MyCompanyName__. All rights reserved.
//

#import "YPLoginBaseClass.h"
#import "YPLoginUserInfo.h"


NSString *const kYPLoginBaseClassStatus = @"status";
NSString *const kYPLoginBaseClassUserInfo = @"user_info";
NSString *const kYPLoginBaseClassMsg = @"msg";


@interface YPLoginBaseClass ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation YPLoginBaseClass

@synthesize status = _status;
@synthesize userInfo = _userInfo;
@synthesize msg = _msg;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.status = [self objectOrNilForKey:kYPLoginBaseClassStatus fromDictionary:dict];
            self.userInfo = [YPLoginUserInfo modelObjectWithDictionary:[dict objectForKey:kYPLoginBaseClassUserInfo]];
            self.msg = [self objectOrNilForKey:kYPLoginBaseClassMsg fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.status forKey:kYPLoginBaseClassStatus];
    [mutableDict setValue:[self.userInfo dictionaryRepresentation] forKey:kYPLoginBaseClassUserInfo];
    [mutableDict setValue:self.msg forKey:kYPLoginBaseClassMsg];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.status = [aDecoder decodeObjectForKey:kYPLoginBaseClassStatus];
    self.userInfo = [aDecoder decodeObjectForKey:kYPLoginBaseClassUserInfo];
    self.msg = [aDecoder decodeObjectForKey:kYPLoginBaseClassMsg];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_status forKey:kYPLoginBaseClassStatus];
    [aCoder encodeObject:_userInfo forKey:kYPLoginBaseClassUserInfo];
    [aCoder encodeObject:_msg forKey:kYPLoginBaseClassMsg];
}

- (id)copyWithZone:(NSZone *)zone {
    YPLoginBaseClass *copy = [[YPLoginBaseClass alloc] init];
    
    
    
    if (copy) {

        copy.status = [self.status copyWithZone:zone];
        copy.userInfo = [self.userInfo copyWithZone:zone];
        copy.msg = [self.msg copyWithZone:zone];
    }
    
    return copy;
}


@end
